//
//  FanqieApiHelper.m
//  MeToo
//
//  Created by 俊松何 on 2018/10/9.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "FanqieApiHelper.h"
static FanqieApiHelper *fanqiehelper = nil;
@implementation FanqieApiHelper

+(instancetype)helper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fanqiehelper = [[FanqieApiHelper alloc] init];
    });
    return fanqiehelper;
}
-(void)initializeFanqie
{
    [PLStreamingEnv initEnv];
}

//0,未知    1，男    2，女
-(void)registerUserAvatar:(NSString *)avatar
                     name:(NSString *)name
                    phone:(NSString *)phone
              countryCode:(NSString *)countryCode
                   gender:(NSString *)gender
               completion:(void (^)(MTUserModel* user, MTError *mterror))completionBlock
{
    NSString *md5Str = [NSString stringWithFormat:@"avatar=%@&countryCode=%@&gender=%@&name=%@&phone=%@&secretKey=userinfoxxx",avatar,countryCode,gender,name,phone];
    NSString *secret = [[MTHttpTool shareTool] MD5StringForString:md5Str];
    NSDictionary *dict = @{@"avatar":avatar,
                           @"countryCode":countryCode,
                           @"gender":gender,
                           @"name":name,
                           @"phone":phone,
                           @"secret":secret
                           };
    [[MTHttpTool shareTool] postPath:@"/user/secretRegisterLogin" parameters:dict success:^(MTBaseData *datamodel) {
        MTUserModel *model = [MTUserModel mj_objectWithKeyValues:datamodel.data];
        [MTUserModel saveCurrentUser:model];
        LTUser *ltuser = [LTSDKHelper usertransformMTUser:model];
        [[LTClient sharedClient] loginWithUser:ltuser];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 返回直播聚合页面控制器
 
 @return LiveContainerController
 */
-(MTLiveContainerController *)liveContainerController
{
    MTLiveContainerController *liveVC = [MTLiveContainerController containerVc];
    return liveVC;
}


/**
 弹出开播控制器
 
 @param controller fromVc 用那个控制器额将开播控制器弹出
 */
-(void)presentCreateLiveVcFromVc:(UIViewController *)controller
{
    [[MTUserApiManager manager] getUserAuthenticationinfoWithcompletion:^(UserAuthenticationinfo *info, MTError *mterror) {
        if(mterror == nil)
        {
            if(info.status == AuthenticationStatusPass)
            {
                HLiveController *liveVc = [[HLiveController alloc] initWithType:LiveControllertypeLive];
                MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:liveVc];
                [controller presentViewController:navVc animated:YES completion:nil];
            }else if(info.status == AuthenticationStatusNone)
            {
                MTWebViewController *webVc = [[MTWebViewController alloc] init];
                webVc.url = info.authenticationUrl;
                MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:webVc];
                [controller presentViewController:navVc animated:YES completion:nil];
                
            }else if(info.status == AuthenticationStatusWaitAuth)
            {
                UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"实名认证审核中，审核通过后才能开播" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alterVc addAction:action1];
                [controller presentViewController:alterVc animated:YES completion:nil];
            }else if (info.status == AuthenticationStatusNoPass)
            {
                UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"实名认证失败，请重新认证" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    MTWebViewController *webVc = [[MTWebViewController alloc] init];
                    webVc.url = info.authenticationUrl;
                    MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:webVc];
                    [controller presentViewController:navVc animated:YES completion:nil];
                }];
                [alterVc addAction:action1];
                [controller presentViewController:alterVc animated:YES completion:nil];
            }
        }
    }];
}

-(MTWalletViewController *)wallteVc
{
   return [[MTWalletViewController alloc] init];
}

-(MTLiveManagerListController *)liveManagerVc
{
    MTLiveManagerListController *managerVc = [[MTLiveManagerListController alloc] init];
    managerVc.type = 1;
    managerVc.liveUserId = [LTClient sharedClient].currentUserId;
    return managerVc;
}





@end
